package com.longnows.appserver.model;

import com.fasterxml.jackson.core.type.TypeReference;
import com.longnows.appserver.utils.GenericAndJson;
import com.longnows.appserver.utils.ListAndJson;
import com.longnows.appserver.utils.MapAndJson;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Entity
@Getter
@Setter
public class Sku extends BaseEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private BigDecimal price;

    private BigDecimal discountPrice;

    private Boolean online;

    private String img;

    private String title;

    private Long spuId;

//    @Convert(converter = ListAndJson.class)
//    private List<Object> specs;

//    @Convert(converter = MapAndJson.class)
//    private Map<String, Object> test;

    public List<Spec> getSpecs() {
        if (this.specs == null){
            return Collections.emptyList();
        }
        return GenericAndJson.JsonToObject(this.specs, new TypeReference<List<Spec>>() {
        });
    }

    public void setSpecs(String specs) {
        if (specs.isEmpty()){
            return;
        }
        this.specs = GenericAndJson.objectToJson(specs);
    }

    private String specs;


    private String code;

    private Long stock;

    private Long categoryId;

    private Long rootCategoryId;

}
